export default {
  data() {
    return {
      listLoading: false, //表格是否显示loading
      total: 0, //表格总条数
      pageSize: 10, //表格每页显示数量
      pageIndex: 0, //表格显示第几页
      params: { //表格请求数据对象
        offset: this.startOffsetNum,
        limit: this.pageSize
      }
    }
  },
  methods: {
    handleSizeChange(pageSize) { //监听表格每页显示数量
      console.log(pageSize);
      this.pageSize = pageSize;
    },
    handleCurrentChange(pageIndex) { //监听表格显示第几页
      console.log(pageIndex)
      this.pageIndex = pageIndex - 1;
    },
    showMessage(message, type, duration) { //提示信息共用方法
      this.$message({
        message: message,
        type: type || 'success', //success warning error
        duration: duration || 3 * 1000
      })
    },
    showConfirmMessage(desc, htmlFlag = false, title = "温馨提示") { //提示信息共用方法
      this.$alert(desc, title, {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: htmlFlag
      });
    }
  },
  computed: {
    startOffsetNum: function() { //计算第几页开始查的索引值
      return this.pageIndex * this.pageSize;
    }
  },
  filters: {
    genderFilter(gender) { //数据文本格式转化
      const genderMap = {
        M: '男',
        F: '女',
      }
      return genderMap[gender];
    },
    statusFilter(status) { //数据文本格式转化
      const statusMap = {
        0: '启用',
        1: '未启用'
      }
      return statusMap[status];
    },
    deletableFilter(flag) { //数据文本格式转化
      const deletableMap = {
        false: '否',
        true: '是'
      }
      return deletableMap[flag];
    },
    nodeTypeFilter(nodeType) { //节点类型格式转化
      const nodeTypeMap = {
        0: '设备',
        1: '网关'
      }
      return nodeTypeMap[nodeType];
    },
    crtimeFilter(crtime) { //时间类型格式转化
      function addPrefix(value) { //补充增加时间前缀
        return value < 10 ? '0' + value : value;
      }
      let times = "";
      if (crtime) {
        let d = new Date(crtime);
        times = d.getFullYear() + '-' + addPrefix((d.getMonth() + 1)) + '-' + addPrefix(d.getDate()) + ' ' + addPrefix(d.getHours()) + ':' + addPrefix(d.getMinutes()) + ':' + addPrefix(d.getSeconds());
      }

      return times;
    },
    deviceStatusFilter(status) { //数据文本格式转化
      const deviceStatusMap = {
        'ONLINE': '设备在线',
        'OFFLINE': '设备离线',
        'UNACTIVE': '设备未激活',
        'DISABLE': '设备已禁用'
      }
      return deviceStatusMap[status];
    },
    reviewStatusFilter(status) { //审核文本格式转化
      const reviewStatusMap = {
        '0': '等待中',
        '1': '已通过',
        '-1': '已拒绝'
      }
      return reviewStatusMap[status];
    }
  }
}
